<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Companies extends CI_Model {

    var $company_name   = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function getAllCompaniesForUser($company_id)
    {      
        $this->db->order_by('company_name','ASC');
        $this->db->where('id',$company_id);
        //$this->db->join('sectors', 'companies.id = sectors.company_id');
        $query = $this->db->get('companies');        
        return $query->result();
    }

    function getAllCompanies()
    {      
        $this->db->order_by('company_name','ASC');
        //$this->db->join('sectors', 'companies.id = sectors.company_id');
        $query = $this->db->get('companies');        
        return $query->result();
    }

    function getSectors($company_id)
    {   
        $this->db->from('sectors');
        $this->db->where('company_id',$company_id);
        $query = $this->db->get();
        return $query->result();
    }
    function getSector($sector_id){
        $this->db->from('sectors');
        $this->db->where('id',$sector_id);
        $query = $this->db->get();
        return $query->result();
    }

    function getCompanies($row){
        $this->db->order_by('company_name','ASC');
        $query=$this->db->get('companies',3,$row);
        if($query->num_rows()>0){
            return $query->result();
            
        }
    }
    function getNumCompanies(){
        return $this->db->count_all('companies');
    }
    function insertCompany()
    {
        $this->company_name   = $this->input->post('company_name');
        $this->db->insert('companies', $this);
    }
    function addSektor($id)
    {
        $sektor['company_id'] = $id;
        $sektor['sector_name'] = $this->input->post('sektor_name');
        $this->db->insert('sectors', $sektor);
    }
    function updateCompany($id)
    {
        $update_data = array(
                'company_name' => $this->input->post('company_name'),
            );
            $this->db->where('id',$id);
            $update = $this->db->update('companies', $update_data);
    }

    function removeCompany()
    {
        $id = $this->input->post('id');
        $this->db->where('id',$id);
        $this->db->delete('companies');
    }

    function removeSektor($sector_id)
    {        
        $this->db->where('id',$sector_id);
        $this->db->delete('sectors');
    }

    function getCompany($id){
        $this->db->from('companies');
        $this->db->where('id',$id);
        $query = $this->db->get();
        return $query->row();
    }

    function getLimitedSectors($company_id,$sector_id){
         $this->db->from('sectors');
        $this->db->where(array('company_id'=>$company_id,'id'=>$sector_id));
        $query = $this->db->get();
        return $query->result();
    }

    
}
?>